package org.niux.aims.action;

import java.util.List;

import javax.annotation.Resource;

import org.niux.aims.common.web.AimsBaseAction;
import org.niux.aims.service.IAccountService;
import org.niux.aims.service.IApplicationService;
import org.niux.aims.service.ILogsService;
import org.niux.aims.vo.AccountVO;
import org.niux.aims.vo.ApplicationVO;

/**
 * 
 * Copyright (c) 2009,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：EditAccountAction.java<br>
 * 摘    要：编辑账户数据页面类<br>
 * <br>
 * 当前版本：<br>
 * 作    者：铭显 Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2013-8-27<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：@since Oct 22, 2009<br>
 */
public class EditAccountAction extends AimsBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3821076455281453066L;

	@Resource(name="applicationService")
	private IApplicationService applicationService;
	
	@Resource(name="accountService")
	private IAccountService accountService;
	
	@Resource(name="logsService")
	private ILogsService logsService;
	
	private Integer id;
	private String action;
	private String app;
	private String accountName;
	private String password;
	private AccountVO accountVo;
	private List<ApplicationVO> applications;
	
	/**
	 * 
	 * 方法概述：页面渲染
	 * 描述：页面渲染
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-8-27
	 */
	public String showPage(){
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】进入账户编辑界面");
		
		if(id != null){
			accountVo = accountService.findById(id);
		} else {
			accountVo = null;
		}
		
		applications = applicationService.findAll();
		action = "";
		
		return SUCCESS;
	}
	
	/**
	 * 
	 * 方法概述：新增账户
	 * 描述：新增账户
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-8-27
	 */
	public String addAccount(){
		List<AccountVO> accounts = accountService.findByNameAndApp(accountName, Integer.parseInt(app));
		
		if(!accounts.isEmpty()){
			action = "AccountAlreadyExists";
		} else {
			AccountVO account = new AccountVO();
			
			account.setName(accountName);
			account.setPassword(password);
			
			ApplicationVO appVo = new ApplicationVO();
			appVo.setId(Integer.parseInt(app));
			
			account.setApp(appVo);
			
			accountService.save(account);
			
			logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】新增账户【"+accountName+"】");
		}
		
		return SUCCESS;
	}
	
	/**
	 * 
	 * 方法概述：修改账户
	 * 描述：修改账户
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-8-27
	 */
	public String updateAccount(){
		AccountVO account = new AccountVO();
		
		account.setId(id);
		account.setName(accountName);
		
		ApplicationVO appVo = new ApplicationVO();
		appVo.setId(Integer.parseInt(app));
		
		account.setApp(appVo);
		
		accountService.update(account);
		
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】修改账户【"+accountName+"】");
		
		action = "Update";
		return SUCCESS;
	}
	
	/**
	 * 
	 * 方法概述：修改账户密码
	 * 描述：修改账户密码
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-9-2
	 */
	public String updatePassword(){
		AccountVO account = new AccountVO();
		
		account.setId(id);
		account.setPassword(password);
		
		accountService.update(account);
		
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】修改账户【"+accountName+"】密码");
		
		action = "UpdatePassword";
		return SUCCESS;
	}
	
	/**
	 * 
	 * 方法概述：删除账户数据
	 * 描述：删除账户数据
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-8-27
	 */
	public String del(){
		AccountVO account = accountService.findById(id);
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】删除账户【"+account.getName()+"】");
		
		accountService.delete(id);
		action = "del";
		return SUCCESS;
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}
	public String getApp() {
		return app;
	}
	public void setApp(String app) {
		this.app = app;
	}
	public String getAccountName() {
		return accountName;
	}
	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public AccountVO getAccountVo() {
		return accountVo;
	}
	public void setAccountVo(AccountVO accountVo) {
		this.accountVo = accountVo;
	}

	public List<ApplicationVO> getApplications() {
		return applications;
	}

	public void setApplications(List<ApplicationVO> applications) {
		this.applications = applications;
	}
	
}
